package com.can.Controller;


import com.can.Service.EsService;
import com.can.Service.Impl.GameStoreServiceImpl;
import com.can.domain.vo.GameStoreVo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/gamestore")
@RestController
public class gamestorecontroller {
    @Autowired
    GameStoreServiceImpl gameStoreServiceImpl;
    @Autowired
    EsService searchService;

    @GetMapping("/GetAllGames")
    public GameStoreVo GetAllGames() {
        return gameStoreServiceImpl.GetAllGames();
    }

    @GetMapping("/GetGamedetail/{id}")
    public GameStoreVo GetGamedetail(@PathVariable("id") int id) {
        System.out.println("查找游戏详情id：" + id);
        return gameStoreServiceImpl.GetGamedetail(id);
    }

    @GetMapping("Search/{content}")
    public GameStoreVo Search(@PathVariable("content") String content) throws IOException {
        System.out.println("搜索游戏：" + content);
        return gameStoreServiceImpl.SearchGame(content);
    }

    @PostMapping("/deleteGame/{id}")
    @ResponseBody
//    删除游戏
    public GameStoreVo deletePost(HttpServletRequest request, @PathVariable(value = "id") int postid) {
        String power = request.getHeader("X-Power");
        if (power==null || power.equals("")||power.equals("0") ){
            return new GameStoreVo<>(403, "权限错误", null);
        } else {
            return gameStoreServiceImpl.deleteGameById(postid);
        }
    }

    @PostMapping("/addGame")
    @ResponseBody

    public GameStoreVo addGame(HttpServletRequest request,@RequestParam("file") MultipartFile file,
                           @RequestParam("title") String name,
                           @RequestParam("content") String content
    ) {
        System.out.println("添加游戏");
        String power = request.getHeader("X-Power");
        int  userId = Integer.parseInt(request.getHeader("X-UserId"));
        String userName = request.getHeader("X-UserName");
        System.out.printf("用户名:%s，id:%s",userName,userId);
        if (power==null || power.isEmpty() ||power.equals("0") ){
            return new GameStoreVo<>(403, "权限错误", null);
        } else {
            try {
                return gameStoreServiceImpl.addGame(userId,name,content,file);
            }catch (Exception e){
                return  new GameStoreVo<>(500,"提交失败",e.getMessage());
            }

        }
//            -----------------------------

    }

}

